/*
 * (C) Copyright 2012, by Tim Xie.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.superapk.diamond.sqllite;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

import com.superapk.diamond.util.LogUtil;

/**
 * 数据库的字段
 * 
 * @author     Tim Xie [tim.xie2008@gmail.com]
 * @date 2013-3-14
 * 
 */

class FieldInfo {

	/**
	 * 类属性名
	 */
	private String fieldName;
	/**
	 * 数据库字段名
	 */
	private String columnName;
	/**
	 * 默认值
	 */
	private String defaultValue;
	/**
	 * 是否是主键
	 */
	private boolean isPrimeKey = false;
	/**
	 * 数据类型
	 */
	private Class<?> dataType;
	/**
	 * 是否是外键
	 */
	private boolean isForeign = false;
	/**
	 * 如果有外键，外键的值
	 */
	private FieldInfo foreignFieldInfo;
	/**
	 * 是否是唯一值
	 */
	private boolean unique = false;
	/**
	 * 
	 */
	private Field field;
	/**
	 * 当前属性的Get方法
	 */
	private Method get;
	/**
	 * 当前属性的Set方法
	 */
	private Method set;

	String getFieldName() {
		return fieldName;
	}

	void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	String getColumnName() {
		return columnName;
	}

	void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	String getDefaultValue() {
		return defaultValue;
	}

	void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	boolean isPrimeKey() {
		return isPrimeKey;
	}

	void setPrimeKey(boolean isPrimeKey) {
		this.isPrimeKey = isPrimeKey;
	}

	Class<?> getDataType() {
		return dataType;
	}

	void setDataType(Class<?> dataType) {
		this.dataType = dataType;
	}

	boolean isForeign() {
		return isForeign;
	}

	void setForeign(boolean isForeign) {
		this.isForeign = isForeign;
	}

	FieldInfo getForeignFieldInfo() {
		return foreignFieldInfo;
	}

	void setForeignFieldInfo(FieldInfo foreignFieldInfo) {
		this.foreignFieldInfo = foreignFieldInfo;
	}

	boolean isUnique() {
		return unique;
	}

	void setUnique(boolean unique) {
		this.unique = unique;
	}
	
	Method getGet() {
		return get;
	}

	void setGet(Method get) {
		this.get = get;
	}

	Method getSet() {
		return set;
	}

	void setSet(Method set) {
		this.set = set;
	}

	/**
	 * 获取某个实体执行某个方法的结果
	 * @param obj
	 * @param method
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getValue(Object entity){
		if(entity != null && get != null) {
			try {
				return (T)get.invoke(entity);
			} catch (IllegalArgumentException e) {
				LogUtil.e(e.toString());
			} catch (IllegalAccessException e) {
				LogUtil.e(e.toString());
			} catch (InvocationTargetException e) {
				LogUtil.e(e.toString());
			}
		}
		return null;
	}
	
	public void setValue(Object receiver , Object value){
		if(set != null && value != null){
			try {
				if (dataType == String.class) {
					set.invoke(receiver, value.toString());
				} else if (dataType == int.class || dataType == Integer.class) {
					set.invoke(receiver, value == null ? (Integer) null : Integer.parseInt(value.toString()));
				} else if (dataType == float.class || dataType == Float.class) {
					set.invoke(receiver, value == null ? (Float) null : Float.parseFloat(value.toString()));
				} else if (dataType == double.class || dataType == Double.class) {
					set.invoke(receiver, value == null ? (Double) null: Double.parseDouble(value.toString()));
				} else if (dataType == long.class || dataType == Long.class) {
					set.invoke(receiver, value == null ? (Long) null : Long.parseLong(value.toString()));
				} else if (dataType == java.util.Date.class || dataType == java.sql.Date.class) {
					set.invoke(receiver, value == null ? (Date) null : DateFormatUtil.stringToDateTime(value.toString()));
				} else if (dataType == boolean.class || dataType == Boolean.class) {
					set.invoke(receiver, value == null ? (Boolean) null: "1".equals(value.toString()));
				} else {
					set.invoke(receiver, value);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			try {
				field.setAccessible(true);
				field.set(receiver, value);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
